//
//  TodoViewController.swift
//  Todo-List
//
//  Created by stu on 2019/9/23.
//  Copyright © 2019年 stu. All rights reserved.
//

import UIKit

class TodoViewController: UITableViewController,AddItemViewControllerDelegate {
    var category:TodoCategory!
 
    
    required init?(coder aDecoder: NSCoder) {
        
        
        super.init(coder: aDecoder)
        
    }

   
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if let category = category {
            title = category.name
        }
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return category.items.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TodolistItem", for: indexPath)
        let textLable = cell.viewWithTag(1000) as! UILabel
        
        let item = category.items[indexPath.row]
        textLable.text = item.text
        configCellCheckmark(for: cell, at: indexPath)
        
        return cell

    }
    
    func configCellCheckmark(for cell:UITableViewCell,at indexPath:IndexPath) {
        let checkLable = cell.viewWithTag(1001) as! UILabel
        
        let item = category.items[indexPath.row]
        if item.checked {
            checkLable.text = "V"
        }else {
            checkLable.text = ""
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) {
          let item = category.items[indexPath.row]
            item.checked = !item.checked
            configCellCheckmark(for: cell, at: indexPath)
            
            tableView.deselectRow(at: indexPath, animated: true)

        }
        }
    
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        category.items.remove(at: indexPath.row)
        let path = [indexPath]
        tableView.deleteRows(at: path, with: .automatic)
        
        
    }
    
    
    
    func AddItemViewController(_ controller: AddItemViewController, endAdding item: TodoItem) {
        let newRow = category.items.count
        category.items.append(item)
        
        
        let indexPath = IndexPath(row: newRow, section: 0)
        let paths = [indexPath]
        tableView.insertRows(at: paths, with: .automatic)
        
       
    }
    func AddItemViewController(_ controller: AddItemViewController, endEditing item: TodoItem) {
        if let index = category.items.firstIndex(where:{$0 === item}) {
            category.items[index] = item
            
            let indexPath  = IndexPath(row:index,section:0)
            if let cell = tableView.cellForRow(at:indexPath) {
                let textLable = cell.viewWithTag(1000) as! UILabel
                textLable.text = item.text
            }
        }
        
       
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "AddItem" {
            let dest = segue.destination as! UINavigationController
            let addItemViewController = dest.topViewController as! AddItemViewController
            addItemViewController.delegate = self
            
            
        }else if segue.identifier == "EditItem" {
            let dest = segue.destination as! UINavigationController
            let addItemViewController = dest.topViewController as! AddItemViewController
            addItemViewController.delegate = self
            if let index = tableView.indexPath(for: sender as! UITableViewCell) {
               addItemViewController.itemToEdit = category.items[index.row]
            }
            
        }
    }
    
    
        
    }
    

